<?php
/**
 * 功能: 图片或视频上传
 * User: 文丹
 * Date: 2018/5/17 0017
 * Time: 16:40
 */

namespace app\admin\controller;
use app\base\controller\Power;
use think\Request;


class Uploads extends Power{

    /**
     *  图片上传
     */
    public function index(){
        $request =Request ::instance();
        $file = $request->file('file');
        if(empty($file)){
            return json(['msg'=>'没有接受到数据','ok'=>false]);
        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size' => 2097152, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');

        if ($info) {
            $img =   $info->getSaveName();;
            $img1 = str_replace("\\","/",$img);
            return json(['msg'=>'上传成功','ok'=>true,'img'=>$img1]);
        } else {
            $error = $file->getError();
            return json(['msg'=>'上传失败'.$error,'ok'=>false]);


        }
    }

    /**
     * 视频上传
     */
    public function video(){
        $request =Request ::instance();
        $file = $request->file('file');
        if(empty($file)){
            return json(['msg'=>'没有接受到数据','ok'=>false]);
        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size' => '31457280', 'ext' => 'wmv,mp4'])->move(ROOT_PATH . 'public' . DS . 'uploads'. DS . 'video');

        if ($info) {
           // $url = 'http://'.$_SERVER['HTTP_HOST'].'/buy/public/uploads/video';
            $img =   $info->getSaveName();;
            $img1 = str_replace("\\","/",$img);
           // $image = $url.'/'.$img1;
            return json(['msg'=>'上传成功','ok'=>true,'video'=>$img1]);
        } else {
            $error = $file->getError();
            return json(['msg'=>$error,'ok'=>false]);
        }
    }

}